package com.dongan.filter;

import com.dongan.pojo.SecurityUser;
import com.dongan.pojo.UserLogin;
import com.dongan.utils.JwtUtils;
import com.dongan.utils.ResponseUtil;
import com.dongan.utils.ResultResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName:TokenLoginFilter
 * @Description:认证过滤器
 * @Author:longtao
 * @Date:2021/6/28
 * @Version:1.0
 */
public class TokenLoginFilter extends UsernamePasswordAuthenticationFilter {

    private AuthenticationManager authenticationManager;

    public TokenLoginFilter(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
        this.setPostOnly(false);
        this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/user/login","POST"));
    }


    /**
     * 获取表单提交的用户名和密码
     * @param request
     * @param response
     * @return
     * @throws AuthenticationException
     */
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        System.out.println("经过了TokenLoginFilter中的attemptAuthentication方法");
        //获取表单提交的内容
            ObjectMapper mapper=new ObjectMapper();
        UserLogin user = null;
        try {
            user = mapper.readValue(request.getInputStream(), UserLogin.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword(), new ArrayList<>());
            return authenticationManager.authenticate(token);
    }


    /**
     * 身份认证失败!
     * @param request
     * @param response
     * @param failed
     * @throws IOException
     * @throws ServletException
     */
    @Override
    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
        System.out.println("经过了TokenLoginFilter中的unsuccessfulAuthentication方法");
        ResultResponseUtil util=new ResultResponseUtil(false,"登录失败!");
        ResponseUtil.out(response,util);
    }


    /**
     * 身份认证成功
     * @param request
     * @param response
     * @param chain
     * @param authResult
     * @throws IOException
     * @throws ServletException
     */
    @Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
        System.out.println("经过了TokenLoginFilter中的successfulAuthentication方法");

        //认证成功之后,得到认证成功之后的用户信息
        SecurityUser  user= (SecurityUser) authResult.getPrincipal();
        //根据用户名生成token
        String jwtToken = JwtUtils.getJwtToken(String.valueOf(user.getUserInfo().getId()), user.getUserInfo().getLoginId());
        ResultResponseUtil util=new ResultResponseUtil(true,"登录成功!",jwtToken);
        ResponseUtil.out(response,util);
    }
}
